package com.jic.manager.controller.manage.conf;


import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.SysConfig;
import com.jic.conf.request.SysConfigRequest;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.SysConfigService;
import com.jic.manager.utils.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sysConfig")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;


    @PostMapping("getSysConfigPageList")
    @ResponseBody
    public RestResult<PageResult<SysConfig>> getSysConfigPageList(@RequestBody SysConfigRequest sysConfigRequest){
        try {
            PageResult<SysConfig> sysConfigPageResult = sysConfigService.queryPage(sysConfigRequest);
            return new RestResult<PageResult<SysConfig>>(){{
               setCode("0000");
               setData(sysConfigPageResult);
            }};
        }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
    }

    @PostMapping("updateSysConfigInfo")
    public RestResult<String> updateSysConfigInfo(@RequestBody SysConfigRequest sysConfigRequest){
        try{
            if (null != sysConfigRequest.getId()){
                //修改
                sysConfigService.update(sysConfigRequest);
            }else{
                //新增
                sysConfigService.insert(sysConfigRequest);
            }
            return new RestResult<String>(){{setCode(ResponseCode.SUCCESS);}};
        }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
    }

    @PostMapping("deleteSysConfigByIds")
    public RestResult deleteSysConfigByIds(@RequestBody SysConfigRequest sysConfigRequest){
        try {
            sysConfigService.delete(sysConfigRequest);
            return new RestResult(){{
                setCode("0000");
                setCode(ResponseCode.SUCCESS);
            }};
        }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
    }

    /**
     * 根据ID查询单个字典表对象
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询单个字典表对象")
    @ResponseBody
    @RequestMapping("/getInfoById")
    public RestResult<SysConfig> getInfoById(Long id){
        RestResult restResult = new RestResult();
        try {
            if (id==0){
                throw new Exception("必传参数为空 id");
            }
            SysConfig sysConfig = sysConfigService.queryByPrimaryKey(id);
            restResult=RestResult.success(sysConfig);
        }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }

    /**
     * 根据key查询单个字典表
     * @param sysConfigRequest
     * @return
     */
    @ApiOperation(value = "根据key查询单个字典表")
    @ResponseBody
    @RequestMapping("/getInfoByKey")
    public RestResult<SysConfig> getInfoByKey(@RequestBody SysConfigRequest sysConfigRequest){
        RestResult restResult = new RestResult();
        try {
            if (sysConfigRequest==null){
                throw new Exception("必传参数为空 key");
            }
            List<SysConfig> sysConfig= sysConfigService.query(BeanUtil.copy(sysConfigRequest,SysConfig.class));
            restResult=RestResult.success(sysConfig);
        }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }

    @ApiOperation(value = "根据keys修改 sysConfig")
    @ResponseBody
    @RequestMapping("/updateSysConfigInfoByKey")
    public RestResult<String> updateSysConfigInfoByKey(@RequestBody SysConfigRequest sysConfigRequest ){
        try{
             sysConfigService.updateCommonList(sysConfigRequest);
        return new RestResult<String>(){{setCode("0000");}};
    }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
    }


}
